AddOrUpdate Method (TKey, Func(TKey, TValue), Func(TKey, TValue, TValue))

Task Parallel System.Threading

Adds a key/value pair to the ConcurrentDictionary<(Of <(TKey, TValue>)>) if the key does not already exist, or updates a key/value pair in the ConcurrentDictionary<(Of <(TKey, TValue>)>) if the key already exists.

Namespace:  System.Collections.Concurrent
Assembly:  System.Threading (in System.Threading.dll)

Syntax

Visual Basic (Declaration)
Public Function AddOrUpdate ( _
	key As TKey, _
	addValueFactory As Func(Of TKey, TValue), _
	updateValueFactory As Func(Of TKey, TValue, TValue) _
) As TValue
C#
public TValue AddOrUpdate(
	TKey key,
	Func<TKey, TValue> addValueFactory,
	Func<TKey, TValue, TValue> updateValueFactory
)

Parameters

key
Type: TKey
The key to be added or whose value should be updated
addValueFactory
Type: System..::.Func<(Of <(TKey, TValue>)>)
The function used to generate a value for an absent key
updateValueFactory
Type: System..::.Func<(Of <(TKey, TValue, TValue>)>)
The function used to generate a new value for an existing key based on the key's existing value

Return Value

The new value for the key. This will be either be the result of addValueFactory (if the key was absent) or the result of updateValueFactory (if the key was present).

Exceptions

ExceptionCondition
System..::.ArgumentNullExceptionkey is a null reference (Nothing in Visual Basic).
System..::.ArgumentNullExceptionaddValueFactory is a null reference (Nothing in Visual Basic).
System..::.ArgumentNullExceptionupdateValueFactory is a null reference (Nothing in Visual Basic).
System..::.OverflowExceptionThe dictionary contains too many elements.

See Also